package com.bw.club

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Observer
import com.bw.BaseActivity
import com.bw.club.databinding.ActivityMainBinding
import com.bw.club.model.entity.CarEntity
import com.bw.club.viewModel.CarViewModel
import com.bw.logger.Logger
import com.bw.logger.common.LogLevel
import com.bw.logger.common.LogType
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


@AndroidEntryPoint
class MainActivity : BaseActivity() {

    private val logger: Logger by lazy {
        Logger.Builder()
            .setDebug(true)
            .setTag("harmony")
            .setLevel(LogLevel.DEBUG)
            .setLogType(LogType.LOGCAT)
            .build()
    }

    private lateinit var binding: ActivityMainBinding
    private val carViewModel: CarViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)
    }



    override fun handleEvent() {

        binding.ss.setOnClickListener {
            logger.d("MainActivity","this is text log ...")
            CoroutineScope(Dispatchers.IO).launch {
                carViewModel.fetchDatas()
            }
        }

        carViewModel.fetchSuccessState.observe(this,object : Observer<List<CarEntity>>{
            override fun onChanged(value: List<CarEntity>) {
                Toast.makeText(this@MainActivity, "${value.size}", Toast.LENGTH_SHORT).show()
            }

        })

        carViewModel.fetchFailedState.observe(this,object :Observer<Throwable>{
            override fun onChanged(value: Throwable) {
                Toast.makeText(this@MainActivity, "error msg-> ${value.message}", Toast.LENGTH_SHORT).show()
            }

        })
    }

    override fun generateViewBindingRoot(): View {
        binding = ActivityMainBinding.inflate(layoutInflater)
        return binding.root
    }



}